package com.etooling.framework.svo;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author 周志荣
 */
@MappedSuperclass
public abstract class AbstractEntity implements IEntity<String>, Serializable {

    private String id;
    @Transient
    private String key;

    @Transient
    public String getKey() {
        return id;
    }

    @Transient
    public void setKey(String key) {
        this.key = key;
    }

    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(length = 36)
    public String getId() {
        if (id != null && id.trim().equals("")) {
            return null;
        }
        return id;
    }

    public void setId(String id) {
        if (id != null && id.trim().equals("")) {
            this.id = null;
        }
        this.id = id;
    }


    public int hashCode() {
        Object id = getId();
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        // if (getClass() != obj.getClass()){
        // return false;
        // }
        final AbstractEntity other = (AbstractEntity) obj;
        Object id = getId();

        if (id == null) {
            if (other.getId() != null) {
                return false;
            }
        } else if (!id.equals(other.getId())) {
            return false;
        }
        return true;
    }

}
